var server="";

var macAddress="";

var time;
var time1;
var handlerBannerTime;

function show(show_dom) {
    $(".act_item").each(function (index, item) {
        item = $(item);
        if (item.attr("id") == show_dom.attr("id")) {
            item.show();
        } else {
            item.hide();
        }
    });
}
function refreshPrintCode(code) {
    if($("#getcodeerror").is(":visible") || $("#networkerror").is(":visible")  ||  $("#getSpecialCode").is(":visible") ){
        show($(".steps_box"));
    }
    if($("#networkerror").is(":visible")){
        $("#wifi_icon").css({"background-image":"url(images/wifi.png)"});
    }
    $("#code").html(code);
}

function returnServerSignal(ser){
    server=ser;
}

function returnMacSignal(mac) {
    macAddress=mac;
    getBanner();
}

function getBanner(){
    $.post(server,{body:'{"method": "GET_BANNER", "parameter": {"macaddress":"'+macAddress+'"}}'},
        function(json){
            if(json!=null && json!=""){
                if(json.success){
                    if(json.data.banner1!=null && json.data.banner1!=""){
                        document.getElementById("img1").src=json.data.banner1;
                    }
                    if(json.data.banner2!=null && json.data.banner2!=""){
                        document.getElementById("img2").src=json.data.banner2;
                    }
                    if(json.data.banner3!=null && json.data.banner3!=""){
                        document.getElementById("img3").src=json.data.banner3;
                    }

                    if(json.data.smallBanner1!=null && json.data.smallBanner1!=""){
                        document.getElementById("smallImg1").src=json.data.smallBanner1;
                    }
                    if(json.data.smallBanner2!=null && json.data.smallBanner2!=""){
                        document.getElementById("smallImg2").src=json.data.smallBanner2;
                    }
                    if(json.data.smallBanner3!=null && json.data.smallBanner3!=""){
                        document.getElementById("smallImg3").src=json.data.smallBanner3;
                    }
                }
            }
        },"json");

    $.post(server,{body:'{"method": "GET_QR_CODE", "parameter": {"macaddress":"'+macAddress+'"}}'},
        function(json){
            if(json!=null && json!=""){
                if(json.success){
                    document.getElementById("qr_code").src=json.data.qrCode;
                    $("#wifi_icon").css({"background-image":"url(images/wifi.png)"});
                    clearInterval(time);
                }
            }
        },"json");

    $.post(server,{body:'{"method": "GET_CLIENT_CODE", "parameter": {"macaddress":"'+macAddress+'"}}'},
        function(json){
            if(json!=null && json!=""){
                if(json.success){
                    clearInterval(time);
                    document.getElementById("clientCode").innerHTML="设备编号：V"+json.data.clientCode;
                    $('html').css({
                        cursor: 'none'
                    });
                }
            }
        },"json");
}

function downloadSuccess() {
    show($(".success"));
}

function updatePrintProgress(progress) {
    $(document).progressDialog.showDialog(progress+ "%");
    if (progress == 100) {
	    $(document).progressDialog.hideDialog();
        show($(".complete"));
        setTimeout(function () {
                show($(".steps_box"));
        }, 3000);
    }
}

function updatePrintPoto(poto) {
    show($(".view_pic"));
    document.getElementById("printingImg").src=poto;
}

function updatePrintPotoFailure(){
    show($(".dayinerror"));
    console.log("pirnt poto failure");
}

function onGetVerifyCodeFailure(){
    show($(".getcodeerror"));
}

function checkNetWorkFailure(){
    show($(".networkerror"));
    $("#wifi_icon").css({"background-image":"url(images/no_wifi.png)"});
    $("#code").html('');
    $('html').css({
        cursor: ''
    });
}

function getSpecialCode(){
    if($("#success").is(":hidden") ){
        show($(".steps_box"));
    }
    $("#code").html('');

}

function downloadFailure() {
    show($(".failure"));
}

function refreshUI() {
    window.location.reload();
}
function checkAheadNowDate(){
	var now = new Date();
	var year = new Date().getFullYear();
	var month = new Date().getMonth()+1;
	var Minutes=now.getMinutes();
	var Seconds=now.getSeconds();
	var Hours = now.getHours();
	if (month<10){month="0"+month;}
	var today=new Date().getDate();
	if (today<10){ today="0"+today; }
	if(Minutes<10){Minutes="0"+Minutes;}
	if(Hours<10){Hours="0"+Hours;}
	if (Seconds<10){ Seconds="0"+Seconds; }
	var nowTime = year + "-" + month + "-" + today+' '+Hours + ":" + Minutes+":"+ Seconds;
	return nowTime;
}	
function getBannerByTime(){
    if(t2=="#####"){
		var sf=[];
		sf.push("<ul>");
		sf.push("<li><img src=\"images/banner_01.png\"/></li>");
		sf.push("<li><img src=\"images/banner_02.png\"/></li>");
		sf.push("<li><img src=\"images/banner_03.png\"/></li>");
		
		sf.push("</ul>");
		$("#mainpic").html(sf.join(""));	
		$(".slide_container").PicPlay({
			pic: ".main_slide",
			type:3
		});
		clearInterval(time1);
		return;
	}
	var myobj=eval(t2);  
	var s1=checkAheadNowDate();
	var flag=true;
	var sf=[];
	sf.push("<ul>");
	for(var i=0;i<myobj.length;i++){
		var s_start=myobj[i].startTime;
		var s_end=myobj[i].endTime;
		if(s1>=s_start&&s1<s_end){
			sf.push("<li><img src=\""+myobj[i].url+"\"/></li>");
			flag=false;
		} 
	}  
	if(flag){
		sf.push("<li><img src=\"images/banner_01.png\"/></li>");
		sf.push("<li><img src=\"images/banner_02.png\"/></li>");
		sf.push("<li><img src=\"images/banner_03.png\"/></li>");
	}
	sf.push("</ul>");
	$("#mainpic").html(sf.join(""));
	$(".slide_container").PicPlay({
		pic: ".main_slide",
		type:3
	});
	clearInterval(time1);
}

$(document).ready(function () {
    show($(".steps_box"));
    if (typeof(MainSignal) != "undefined") {
        MainSignal.returnMacSignal.connect(returnMacSignal);
        MainSignal.returnServerSignal.connect(returnServerSignal);
        MainSignal.refreshPrintCodeSignal.connect(refreshPrintCode);  //获取验证码
        MainSignal.updatePrintProgressSignal.connect(updatePrintProgress); //打印进度条
        MainSignal.updatePrintPotoSignal.connect(updatePrintPoto);  //打印图片
        MainSignal.downloadSuccessSignal.connect(downloadSuccess); //打印成功
        MainSignal.updatePrintPotoFailureSignal.connect(updatePrintPotoFailure); //打印失败
        MainSignal.getVerifyCodeFailureSignal.connect(onGetVerifyCodeFailure);
        MainSignal.checkNetWorkFailureSignal.connect(checkNetWorkFailure);
        MainSignal.getSpecialCode.connect(getSpecialCode);//获取到特殊验证码
        MainSignal.downloadFailuerSignal.connect(downloadFailure);    // 下载图片失败
        MainSignal.refreshUISignal.connect(refreshUI);    // 更新UI

    }

     var handler = function(){
         if (typeof(MainSignal) != "undefined") {
             getBanner();
         }
     }

    handlerBannerTime = function(){
         getBannerByTime();
     }
    time = setInterval( handler , 30000);
	time1 = setInterval( handlerBannerTime , 500);
})
  